home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / DESKLIBC / SOURCES.ZIP / DeskLib / !DLSources / Libraries / Menu / c / SetText < prev    next >
Text File  |  1995-07-09  |  2KB  |  76 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Menu.SetText.c
  12.     Author:  Copyright © 1993 Shaun Blackmore and Jason Williams
  13.     Version: 1.00 (30 Apr 1993)
  14.     Purpose: Changes the text in a menu item
  15. */
  16.  
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. #include "DeskLib:Wimp.h"
  21. #include "DeskLib:Menu.h"
  22.  
  23.  
  24. void Menu_SetText(menu_ptr menu, int entry, char *text)
  25. {
  26.   int       length, oldlength;
  27.   char      *dest;
  28.   menu_item *item = (menu_item*) (((int) menu) + sizeof(menu_block));
  29.  
  30.   item = &item[entry];
  31.  
  32.   /*  If the new text will not fit into the old buffer, then
  33.    *    if it was indirected
  34.    *      Deallocate the old indirected buffer
  35.    *    Allocate a new buffer
  36.    *  NOTE: we don't bother shrinking the buffer if the new text is shorter
  37.    */
  38.    
  39.   if (item->iconflags.data.indirected)
  40.     oldlength = item->icondata.indirecttext.bufflen; 
  41.   else
  42.     oldlength = wimp_MAXNAME;
  43.  
  44.   if ((length = strlen(text)) >= oldlength)
  45.   {
  46.     if (item->iconflags.data.indirected)
  47.       free(item->icondata.indirecttext.buffer);
  48.  
  49.     item->icondata.indirecttext.buffer = malloc(length + 1);
  50.     if (item->icondata.indirecttext.buffer != NULL)
  51.     {
  52.       item->iconflags.data.indirected         = TRUE;
  53.       item->icondata.indirecttext.bufflen     = length + 1;
  54.       item->icondata.indirecttext.validstring = (char *) -1;
  55.     }
  56.   }
  57.  
  58.   /*  Copy the new string into the buffer, truncating it if it is still
  59.    *  too long to fit into the buffer.
  60.    */
  61.    
  62.   if (item->iconflags.data.indirected)
  63.   {
  64.     length = item->icondata.indirecttext.bufflen - 1;
  65.     dest   = item->icondata.indirecttext.buffer;
  66.   }
  67.   else
  68.   {
  69.     length = wimp_MAXNAME - 1;
  70.     dest   = item->icondata.text;
  71.   }
  72.  
  73.   strncpy(dest, text, length);    /* Copy the string (truncate if neccesary) */
  74.   dest[length] = 0;               /* And ensure it is zero terminated        */
  75. }
  76.